<template>
  <div class="account-login">
    <el-form :model="formInput" :rules="rules" ref="formRef">
      <el-form-item label="账号" prop="username">
        <el-input v-model="formInput.username"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input v-model="formInput.password" show-password></el-input>
      </el-form-item>
    </el-form>
  </div>
</template>

<script setup lang="ts">
import { reactive, ref } from 'vue';
import type {ElForm} from 'element-plus'
import localCache from '@/utils/cache'
import { useLoginStore } from '@/store';
const formInput = reactive({
  username:localCache.getCache('username') || '',
  password:localCache.getCache('password') || ''
})

const rules = reactive({
  username:[
    {
      required: true,
      message: '请输入您的账号',
      trigger: 'blur',
    },
    {
      min: 5,
      max: 12,
      message: '账号长度为5~12',
      trigger: 'blur',
    }
  ],
  password:[
    {
      required: true,
      message: '请输入您的密码',
      trigger: 'blur',
    },
    {
      min: 6,
      max: 12,
      message: '密码长度为6~12',
      trigger: 'blur',
    }
  ]
})
const loginStore = useLoginStore()
const formRef = ref<InstanceType<typeof ElForm>>()
const accountLogin = (isRemember:boolean) => {
  formRef.value?.validate(valide => {
    if(valide){
      if(isRemember){
        localCache.setCache('name',formInput.username)
        localCache.setCache('password',formInput.password)
      }
      loginStore.loginAction({...formInput})
    }
  })
}
defineExpose({
  accountLogin
})
</script>

<style scoped lang="less">

</style>